package misc;

import javax.swing.table.DefaultTableModel;
import java.util.Vector;
import java.util.Arrays;

/**
 * **************************************************************************
 * Application:
 * Author: Erik Stell
 * Date: Feb 8, 2005
 * Time: 3:30:42 PM
 * Process:
 * **************************************************************************
 */
public class SortJTableUtil {
    // Regardless of sort order (ascending or descending), null values always appear last.
    // colIndex specifies a column in model.
    public void sortColumn(DefaultTableModel model, int colIndex, boolean ascending) {
        Vector data = model.getDataVector();
        Object[] colData = new Object[model.getRowCount()];

        // Copy the column data in an array
        for (int i=0; i<colData.length; i++) {
            colData[i] = ((Vector)data.get(i)).get(colIndex);
        }

        // Sort the array of column data
        Arrays.sort(colData, new ColumnSorter(ascending));

        // Copy the sorted values back into the table model
        for (int i=0; i<colData.length; i++) {
            ((Vector)data.get(i)).set(colIndex, colData[i]);
        }
        model.fireTableStructureChanged();
    }
}
